package cn.wh.xwm.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class TimeUtils {
	public static final String DATE_FORMAT_ISO = "yyyy-MM-dd";
	public static final String DATE_FORMAT_ISO_LINE = "MM/dd/yyyy";
	
	
	public static String getCurrentDate(String format) {
		Calendar cal = Calendar.getInstance(TimeZone.getDefault());
		String DATE_FORMAT = format;
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		String currentFormattedDate = sdf.format(cal.getTime()).toString();
		return currentFormattedDate;
	}
	
	/** 
	 * 判断是否是日期格式  或者"MM/dd/yyyy";
	 * @param ds
	 * @return
	 */
	public static boolean isDateFormat(String ds){
		boolean tag = true;
		try {
			fromIsoDate(ds);
		} catch (ParseException e) {
			e.printStackTrace();
			tag = false;
		}
		return tag;
	}
	
	public static String toIsoDate(Date date) {
		String isoDate = null;
		SimpleDateFormat sdf = null;
		
		if (date != null) {
			sdf = new SimpleDateFormat(DATE_FORMAT_ISO);
			isoDate = sdf.format(date);
		}
			
		return isoDate;
	}
	
	public static Date fromIsoDate(String isoDate) throws ParseException  {
		Date date = null;
		SimpleDateFormat sdf = null;
		
		if (isoDate == null) {
			return null;
		}
		
		sdf = new SimpleDateFormat(DATE_FORMAT_ISO);
		date = sdf.parse(isoDate);
		return date;
	}
	public static Date fromIsoDateByLine(String isoDate) throws ParseException  {
		Date date = null;
		SimpleDateFormat sdf = null;
		
		if (isoDate == null) {
			return null;
		}
		
		sdf = new SimpleDateFormat(DATE_FORMAT_ISO_LINE);
		date = sdf.parse(isoDate);
		return date;
	}
	
	/**
	 * 通过连个时间获取他们之间的月份名称
	 * @param from
	 * @param to
	 * @return
	 */
	public static ArrayList<Integer> getMonthesFromTwoDate(Date from,Date to){
		ArrayList<Integer> months = new ArrayList<Integer>();
		Calendar c_begin = new GregorianCalendar();
		Calendar c_end = new GregorianCalendar();
		c_begin.setTime(from);
		c_end.setTime(to);
		while (c_begin.before(c_end)) {
			int m = c_begin.get(Calendar.MONTH) + 1;
			months.add(m);
			System.out.println(parseNumberToMonthCHN(m));
			c_begin.add(Calendar.MONTH, 1);
		}
		return months;
	}
	/**
	 * 数字月份转换中文
	 * @param m
	 * @return
	 */
	public static String parseNumberToMonthCHN(int m) {
		String month="";
		switch (m) {
		case 1:
			month =  Constant.Month_1;
			break;
		case 2:
			month =  Constant.Month_2;
			break;
		case 3:
			month =  Constant.Month_3;
			break;
		case 4:
			month =  Constant.Month_4;
			break;
		case 5:
			month =  Constant.Month_5;
			break;
		case 6:
			month =  Constant.Month_6;
			break;
		case 7:
			month =  Constant.Month_7;
			break;
		case 8:
			month =  Constant.Month_8;
			break;
		case 9:
			month =  Constant.Month_9;
			break;
		case 10:
			month =  Constant.Month_10;
			break;
		case 11:
			month =  Constant.Month_11;
			break;
		case 12:
			month =  Constant.Month_12;
			break;

		}
		return month;
	}

	public static void main(String[] args) {
	}
}
